//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 22
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using ProxyHelpers.EWS;

namespace SetUserOofSettings
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            EmailAddress emailAddress = new EmailAddress();
            emailAddress.Address = "ken.malcolmson@contoso.com";
            SetUserOofSettings(binding, emailAddress);
        }

        /// <summary>
        /// Example of calling the SetUserOofSettingsRequest Web method to
        /// create a scheduled OOF (Listing 22-12)
        /// </summary>
        /// <param name="binding">Binding to make the call with</param>
        /// <param name="userToSetOofFor">EmailAddress of the user
        /// to set the OOF settings for.</param>
        public static void SetUserOofSettings(
            ExchangeServiceBinding binding,
            EmailAddress userToSetOofFor)
        {
            // Assemble the user OOF options, this example will set
            // a scheduled OOF for October 15th, 2007, through October 22nd, 2007, 
            // and set an ExternalReply to be sent to all external recipients
            //
            Duration oofDuration = new Duration();
            oofDuration.StartTime =
                new DateTime(2007, 10, 15, 8, 0, 0, DateTimeKind.Utc);
            oofDuration.EndTime =
                new DateTime(2007, 10, 22, 17, 0, 0, DateTimeKind.Utc);

            ReplyBody internalReply = new ReplyBody();
            internalReply.Message = "I am sorry, but I am out of the office " +
                "from October 15th through the 22nd.  If you need immediate " +
                "assistance, you can e-mail my manager at andy@contoso.com" +
                "\r\n" +
                "Thank you, " +
                "Carol";

            ReplyBody externalReply = new ReplyBody();
            externalReply.Message = "I am sorry, but I am out of the office " +
                "at this time.  I shall attempt to respond to your e-mail " +
                "as soon as I possibly can." +
                "\r\n" +
                "Sincerely, " +
                "Carol Troup";

            UserOofSettings userOofSettings = new UserOofSettings();
            userOofSettings.OofState = OofState.Scheduled;
            userOofSettings.Duration = oofDuration;
            userOofSettings.ExternalAudience = ExternalAudience.All;
            userOofSettings.InternalReply = internalReply;
            userOofSettings.ExternalReply = externalReply;

            // Put together the SetUserOofSettingsRequest
            //
            SetUserOofSettingsRequest setUserOofReq = new
                SetUserOofSettingsRequest();
            setUserOofReq.Mailbox = userToSetOofFor;
            setUserOofReq.UserOofSettings = userOofSettings;

            // Send the request and check for any errors.  In the
            // SetUserOofSettingsResponse, there is one and only
            // one ResponseMessage.
            //
            SetUserOofSettingsResponse setUserOofResponse =
                binding.SetUserOofSettings(setUserOofReq);
            if (setUserOofResponse.ResponseMessage.ResponseClass !=
                ResponseClassType.Success)
            {
                // Indicate that we have a problem
                throw new Exception(String.Format(
                    "Error in setting OOF settings.\r\n{0}\r\n{1}",
                    setUserOofResponse.ResponseMessage.ResponseCode,
                    setUserOofResponse.ResponseMessage.MessageText));
            }
        }

    }
}
